package com.myapidemos.apps.app;

import com.example.myapidemos.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;

public class BroadcastActivicy extends Activity {

	private NotificationManager nmg = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		nmg = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		register();
	}

	@Override
	protected void onResume() {
		super.onResume();
		nmg.cancel(123);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(tool);
	}

	private static String action = "android.intent.action.MAIN";

	private ReceiverTool tool = null;

	private void register() {
		// tool = new ReceiverTool();
		// IntentFilter filter = new IntentFilter();
		// filter.addAction(action);
		// registerReceiver(tool, filter);
		tool = new ReceiverTool();
		IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
		registerReceiver(tool, filter);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		// sendBroadcast(new Intent(action));
	}

	class ReceiverTool extends BroadcastReceiver {

		@SuppressLint("NewApi")
		@SuppressWarnings("deprecation")
		@Override
		public void onReceive(Context context, Intent intent) {
			int version = android.os.Build.VERSION.SDK_INT;
			Notification notification = null;
			PendingIntent intent2 = PendingIntent.getActivity(BroadcastActivicy.this, 0,
					new Intent(BroadcastActivicy.this, BroadcastActivicy.class),
					PendingIntent.FLAG_UPDATE_CURRENT);
			if (version < 11) {
				notification = new Notification(R.drawable.stat_happy, "Ooooooooo",
						System.currentTimeMillis());
				notification.setLatestEventInfo(BroadcastActivicy.this, "click", "sdk version="
						+ android.os.Build.VERSION.SDK_INT + "\nmodel=" + android.os.Build.MODEL,
						intent2);
			} else {
				Builder builder = new Notification.Builder(BroadcastActivicy.this);
				builder.setContentIntent(intent2)
						.setSmallIcon(R.drawable.stat_happy)
						.setWhen(System.currentTimeMillis())
						.setTicker("Zzzzzzzzzzzz")
						.setContentTitle("click")
//						.setDefaults(Notification.DEFAULT_SOUND)
						.setContentText(
								"sdk version=" + android.os.Build.VERSION.SDK_INT + "\nmodel="
										+ android.os.Build.MODEL);
				notification = builder.build();
			}
			nmg.notify(123, notification);
		}
	}
}
